package com.yeyks.cnbooking.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.google.common.collect.Lists;
import com.yeyks.cnbooking.Result.addresssearch.CbCountry;
import com.yeyks.cnbooking.Result.addresssearch.CbCity;
import com.yeyks.cnbooking.Result.addresssearch.CbLocation;
import com.yeyks.cnbooking.Result.addresssearch.CbProvince;
import com.yeyks.cnbooking.service.CnBookingAddressService;
import com.yeyks.other.dal.domain.AddressCode;
import com.yeyks.other.dal.domain.ImportCityDataConfig;
import com.yeyks.other.service.AddressCodeService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;

import java.util.Arrays;
import java.util.List;

/**
 * 地址操作
 *
 * @author: dagny
 * @date: 2019/10/31 0031 17:21
 */
@Service
@Slf4j
public class CnBookingAddressServiceImpl implements CnBookingAddressService {


    @Autowired
    private AddressCodeService addressCodeService;


    @Override
    public void addAddressCode(CbCountry cbCountryDB) {
        List<AddressCode> addressCodeList = null;
        if (null != cbCountryDB && !CollectionUtils.isEmpty(cbCountryDB.getProvince())) {
            addressCodeList = Lists.newArrayList();
            List<CbProvince> provinceList = cbCountryDB.getProvince();
            for (CbProvince province : provinceList) {
                AddressCode addressCodeProvince = new AddressCode();
                addressCodeProvince.setProvinceId(province.getProvinceId());
                addressCodeProvince.setProvinceName(province.getProvinceName());
                List<AddressCode> addressCodeCityList = Lists.newArrayList();
                List<AddressCode> addressCodeLocationList = Lists.newArrayList();
                if (!CollectionUtils.isEmpty(province.getCity())) {
                    for (CbCity city : province.getCity()) {
                        AddressCode addressCityCode = new AddressCode();
                        addressCityCode.setCityId(city.getCityId());
                        addressCityCode.setCityName(city.getCityName());
                        addressCityCode.setProvinceId(province.getProvinceId());
                        addressCityCode.setProvinceName(province.getProvinceName());
                        addressCityCode.setParentId(province.getProvinceId());
                        if (!CollectionUtils.isEmpty(city.getLocation())) {
                            List<AddressCode> addressOneCodeLocationList = Lists.newArrayList();
                            for (CbLocation location : city.getLocation()) {
                                AddressCode addressLocationCode = new AddressCode();
                                addressLocationCode.setCityId(city.getCityId());
                                addressLocationCode.setCityName(city.getCityName());
                                addressLocationCode.setParentId(city.getCityId());
                                addressLocationCode.setProvinceId(province.getProvinceId());
                                addressLocationCode.setProvinceName(province.getProvinceName());
                                addressLocationCode.setLocationId(location.getLocationId());
                                addressLocationCode.setLocationName(location.getLocationName());
                                addressOneCodeLocationList.add(addressLocationCode);
                            }
                            addressCodeLocationList.addAll(addressOneCodeLocationList);
                        }
                        addressCodeCityList.add(addressCityCode);
                    }
                }
                addressCodeList.add(addressCodeProvince);
                addressCodeList.addAll(addressCodeCityList);
                addressCodeList.addAll(addressCodeLocationList);
                addressCodeCityList.clear();
                addressCodeLocationList.clear();
            }

            //批量添加数据
            addressCodeService.saveBatch(addressCodeList);
        }
    }

    /**
     * 查询地址信息
     *
     * @param parentId 父类ID
     * @return
     */
    @Override
    public List<AddressCode> queryCnAddress(String parentId) {

        List<AddressCode> list = null;
        if(StringUtils.isEmpty(parentId)){
            list =addressCodeService.list(new QueryWrapper<AddressCode>()
                    .isNull(AddressCode.PARENT_ID));
        }else{
            list = addressCodeService.list(new QueryWrapper<AddressCode>()
                    .eq(AddressCode.PARENT_ID, parentId));
        }
        return list;
    }

    @Override
    public List<ImportCityDataConfig> queryList(String provinceId, String cityIds) {

        List<ImportCityDataConfig> importCityDataConfigList = Lists.newArrayList();

        if(!StringUtils.isEmpty(cityIds)){
            String[] strings = cityIds.split(",");
            List<AddressCode> addressCodeList = addressCodeService.list(
                    new QueryWrapper<AddressCode>().in(AddressCode.CITY_ID, Arrays.asList(strings))
                            .isNull(AddressCode.LOCATION_ID)
            );
            for(AddressCode addressCode : addressCodeList){
                ImportCityDataConfig importCityDataConfig = new ImportCityDataConfig();
                if(StringUtils.isEmpty(addressCode.getCityId())){
                    continue;
                }
                importCityDataConfig.setCityId(addressCode.getCityId());
                importCityDataConfig.setCityName(addressCode.getCityName());
                importCityDataConfig.setProvinceId(addressCode.getProvinceId());
                importCityDataConfig.setProvinceName(addressCode.getProvinceName());
                importCityDataConfigList.add(importCityDataConfig);
            }
        }

        return importCityDataConfigList;
    }
}
